# -*- coding: utf-8 -*-
'''
Created on Mar 24, 2013

@author: LONG HOANG GIANG
'''

import urllib, json, os, gzip, cStringIO as StringIO
from lxml import etree
import re
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')

def getHtml(url):
    logging.info("load html from {0}".format(url))
    return etree.parse(StringIO.StringIO(urllib.urlopen(url).read()), parser=etree.HTMLParser(encoding='utf-8'))

def extractText(pattern, match, idx, default=''):
    result = default
    try:
        m = re.search(pattern, match)
        if m != None: result = m.group(idx)
    except IndexError:
        pass
    return result

  
def process(url):
    tree = getHtml(url)
    data = []
    for item in tree.xpath("//table[contains(@class, 'tablesorter')]/tbody/tr/td[1]/a"):
        link = 'http://manga24h.com/' + item.get('href')
        title = item.xpath("./text()")[0].strip()
        if type(title).__name__ == 'unicode': title = title.encode('utf-8')
        print '--------------------------------'
        print title, link
        print '--------------------------------'
        itree = getHtml(link)
        images = []
        for image in itree.xpath("//div[@class='row']/div/img"):
            src = image.get('src', '').strip()
            if src == '': continue
            print src
            images.append(src)
        data.append({'chapter': title, 'images': images})
    data.reverse()
    
    path = "/longhoanggiang/comic/{0}".format(extractText(r"/([^/]+)\.html", url, 1))
    
    if not os.path.exists(path):
        os.makedirs(path, 0777)
    fp = gzip.open(path + '/' + 'comic.aac', 'wb')
    json.dump(data, fp)
    fp.close()
    

if __name__ == '__main__':
    
    
    url = raw_input("URL: ")
    if url.startswith("http://"):
        process(url)
        logging.info("finished")
    else:
        logging.warning("invalid url")
    os._exit(1)
    
    
    
    